package com.villaday.idf.util;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.villaday.idf.AppContext;
import com.villaday.idf.base.greendao.FaPiaoTitleEntityDao;
import com.villaday.idf.base.greendao.DaoMaster;
import com.villaday.idf.base.greendao.DaoSession;
import com.villaday.idf.base.greendao.HDao;
import com.villaday.idf.model.FaPiaoTitleEntity;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

/**
 * Created by tudou on 2018/10/17.
 */

public class DbFaPiaoTitleUtils {

    private static DbFaPiaoTitleUtils db;
    private final static String dbName = "dbfapiaotitleutils_db";
    private DaoMaster.DevOpenHelper openHelper;
    private Context context;


    public DbFaPiaoTitleUtils() {
        context = AppContext.getInstance();
        openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
    }


    /**
     * 获取单例
     *
     * @return
     */
    public static DbFaPiaoTitleUtils getInstance() {
        if (db == null) {
            synchronized (DbFaPiaoTitleUtils.class) {
                if (db == null) {
                    db = new DbFaPiaoTitleUtils();
                }
            }
        }
        return db;
    }


    /**
     * 获取可读数据库
     */
    private SQLiteDatabase getReadableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getReadableDatabase();
        return db;
    }

    /**
     * 获取可写数据库
     */
    private SQLiteDatabase getWritableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getWritableDatabase();
        return db;
    }


    public void save(FaPiaoTitleEntity info) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        FaPiaoTitleEntityDao downInfoDao = daoSession.getFaPiaoTitleEntityDao();
        downInfoDao.insert(info);
    }


    public void deleteDowninfo(FaPiaoTitleEntity info) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        FaPiaoTitleEntityDao downInfoDao = daoSession.getFaPiaoTitleEntityDao();
        downInfoDao.delete(info);
    }

    public void deleteAll() {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        FaPiaoTitleEntityDao downInfoDao = daoSession.getFaPiaoTitleEntityDao();
        downInfoDao.deleteAll();
    }


    public List<FaPiaoTitleEntity> queryDownAll() {
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        FaPiaoTitleEntityDao downInfoDao = daoSession.getFaPiaoTitleEntityDao();
        QueryBuilder<FaPiaoTitleEntity> qb = downInfoDao.queryBuilder();
        return qb.list();
    }



    public List<FaPiaoTitleEntity> queryWithSomethingAll(String something) {
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        FaPiaoTitleEntityDao downInfoDao = daoSession.getFaPiaoTitleEntityDao();
        List<FaPiaoTitleEntity> items = downInfoDao.queryBuilder()
                .where(HDao.Properties.Name.like("%"+something + "%"))
                .list();
        return items;
    }


    public void insertOrReplace(String something,String taxpayerNumber) {
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        FaPiaoTitleEntityDao downInfoDao = daoSession.getFaPiaoTitleEntityDao();
        downInfoDao.insertOrReplace(new FaPiaoTitleEntity(something,taxpayerNumber));


    }
}
